package com.tianbing.adlottery.controller.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.jubao.ejb.business.StoreBusiness;
import com.jubao.ejb.dao.CouponDao;
import com.jubao.ejb.entity.CouponItem;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.ControllerFormBean;
import com.tianbing.adlottery.util.UserSession;

public class ConfirmCouponItemController extends SimpleFormController {
	static Logger logger = Logger.getLogger(ConfirmCouponItemController.class.getName());

	private SkinUtils skinUtils;

	StoreBusiness storeBusiness;

	CouponDao couponDao;

	// IPartyService partyService;

	// ------------------GET Request LifeCycle------------------
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		ControllerFormBean fb = new ControllerFormBean();
		long cpisid = ServletRequestUtils.getLongParameter(request, "cpisid", -1);
		CouponItem couponItem = couponDao.findCouponItem(cpisid);
		fb.setCouponItem(couponItem);
		return fb;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
	 *      org.springframework.validation.BindException)
	 */
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,
			BindException ex) throws Exception {
		UserSession auth = (UserSession) skinUtils.getUserAuthorization(request, response);

		ModelAndView mv = new ModelAndView(this.getSuccessView());
		ControllerFormBean form = (ControllerFormBean) cmd;
		CouponItem couponItem = form.getCouponItem();

		couponItem =  storeBusiness.confirmCouponItem(auth.getUserSysId(), couponItem);

		return mv;
	}

	public static void setLogger(Logger logger) {
		ConfirmCouponItemController.logger = logger;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}


	public void setStoreBusiness(StoreBusiness storeBusiness) {
		this.storeBusiness = storeBusiness;
	}

	public void setCouponDao(CouponDao couponDao) {
		this.couponDao = couponDao;
	}

}
